package in.muton.edushare.pages.univ;

import in.muton.edushare.common.UserFilter;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.InnerList;
import in.muton.panels.paginator.EntityListing;
import in.muton.panels.paginator.badgehelpers.user.UnivBadgeRoles;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.vo.Users;

public class UnivPeople extends UnivBase {

	private static final Log log = LogFactory.getLog(UnivPeople.class);

	public UnivPeople(PageParameters params) {
		super(params);
		log.debug("1Number of entries =" + cL.getObject().size());
		pageName = params.getString("1", "UnivEveryone");
		UnivBadgeRoles bRole = new UnivBadgeRoles(getModel());
		add(new EntityListing<Users>("thumbList", new InnerList<Users>() {
			private static final long serialVersionUID = -56856143743214954L;

			@Override
			public List<IModel<Users>> getObject() {
				return getUsers(getPageParameters()).getObject();
			}
		}, bRole, this));
		addPageTitle();
		Form<Void> f = new Form<Void>("filterForm");
		add(f);
		f.add(new TextField<String>("filterString", new PropertyModel<String>(
				this, "searchString")));
	}

	private String searchString = "";

	@Override
	public String getActivePage(PageParameters params) {
		String id = params.getString("1", "UnivEveryone");
		return id;
	}

	@Override
	public String getPageTitle() {
		return getLoggedInUser().getCompleteUserName() + " | "
		+ getModel().getObject().getUniversityName()
		+ " | People | "
		+ ApplicationConstants.APPLICATION_NAME;
	}

	private CayenneList<Users> getUsers(PageParameters params) {
		cL = new CayenneList<Users>(null);
		if (pageName.compareTo("UnivEveryone") == 0) {
			cL = this.ud.getAdmins();
			cL.add(ud.getFaculty());
			cL.add(ud.getStudents());
		} else if (pageName.compareTo("UnivAdmins") == 0) {
			cL.add(ud.getAdmins());
		} else if (pageName.compareTo("UnivProfs") == 0) {
			cL.add(ud.getFaculty());
		} else if (pageName.compareTo("UnivStudents") == 0) {
			cL.add(ud.getStudents());
		}
		cL = UserFilter.filterByString(cL, searchString);
		return cL;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	public String getSearchString() {
		return searchString;
	}

	private CayenneList<Users> cL = new CayenneList<Users>(null);
	private String pageName = "";

	public void addPageTitle() {
		if (pageName.compareTo("UnivEveryone") == 0) {
			add(new Label("peopleLabel", "Everyone"));
		} else if (pageName.compareTo("UnivAdmins") == 0) {
			add(new Label("peopleLabel", "Administrators"));
		} else if (pageName.compareTo("UnivProfs") == 0) {
			add(new Label("peopleLabel", "Faculty"));
		} else if (pageName.compareTo("UnivStudents") == 0) {
			add(new Label("peopleLabel", "Students"));
		} else {
			add(new Label("peopleLabel", ""));
		}
	}
}
